<template>
	<view class="uni-common-pb uni-padding-wrap uni-common-mt">
<!-- 		<view class="uni-form-item uni-column">
			<view class="title">工单服务器设置</view>
			<input class="uni-input" v-model="datas[0]" placeholder="请输入工单服务器IP" />
		</view> -->
		<view class="uni-title uni-common-pl">当前制程选择</view>
		<view class="uni-list">
			<view class="uni-list-cell">
				<view class="uni-list-cell-db">
					<picker @change="bindPickerChange" :value="index" :range="zclist">
						<view class="uni-input">{{zclist[index]}}</view>
					</picker>
				</view>
			</view>
		</view>
		<view class="uni-form-item uni-column">
			<view class="title">机台名称</view>
			<input class="uni-input" v-model="datas[3]" placeholder="请输入机台名称" />
		</view>

		<button type="primary" @tap="setStorage">确认</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				zcdhlist: [],//制程pick  代号
				zcnamelist: [],//名称
				zclist: [],//名称 + 代号
				index: 10,
				keys: ['IP', 'zcdh', 'zcname', 'machine'],
				datas: ['', '', '', '']
			}
		},
		// 跳转到主页面
		onNavigationBarButtonTap(e) {
			uni.reLaunch({
				url: '/pages/main/main'
			});

		},
		onReady: function() {
			this.getStorage();
			//获取制程列表
			uni.request({
				url: this.neiUrl + '/api/workorder/getprocesstype',
				success: (res) => {
					// console.log(JSON.stringify(res.data)); 如果要用jsonP也得区分H5和app
					console.log(JSON.stringify(res.data.data));
					for (var i = 0; i < res.data.data.length; i++) {
						this.zcdhlist[i] = res.data.data[i].daihao;
						this.zcnamelist[i] = res.data.data[i].name;
						this.zclist[i] = res.data.data[i].name + " " + res.data.data[i].daihao;
					}
				},
				fail: (err) => {
					console.log(err);
					uni.showToast({
						title: '获取制程名失败！' + err,
						icon: 'none',
						mask: true,
						duration: 3000
					});
				},
				complete: () => {
					this.index = 0;
					for (var i = 0; i < this.zcdhlist.length; i++) {
						if (this.zcdhlist[i] == this.datas[1]) {
							this.index = i;
							break;
						}
					}
				}
			});
		},
		methods: {
			bindPickerChange: function(e) {
				console.log('picker发送选择改变，携带值为：' + e.target.value)
				this.index = e.target.value
			},
			setStorage: function() {
				this.datas[1] = this.zcdhlist[this.index];
				this.datas[2] = this.zcnamelist[this.index];
				var oknum = 0;
				for (var i = 0; i < this.keys.length; i++) {
					var key = this.keys[i];
					if (key.length === 0) {
						continue;
					} else {
						uni.setStorage({
							key: key,
							data: this.datas[i],
							success: (res) => {
								oknum++;
							},
							fail: () => {

							}
						})
					}
				}
				if (oknum > 0) {
					uni.showToast({
						title: '存储数据成功！',
						icon: 'success',
						mask: true,
						duration: 3000
					});
				} else {
					uni.showToast({
						title: '储存数据失败！',
						icon: 'none',
						mask: true,
						duration: 3000
					});
				}
				// uni.navigateBack();
				uni.reLaunch({
					url: '/pages/main/main'
				});
			},
			getStorage: function() {
				for (var i = 0; i < this.keys.length; i++) {
					var key = this.keys[i];
					if (key.length === 0) {
						continue;
					} else {
						uni.getStorage({
							key: key,
							success: (res) => {
								this.datas[i] = res.data;
							},
							fail: () => {

							}
						})
					}
				}

			},
		}
	}
</script>

<style>
	button {
		margin-top: 30upx;
		margin-bottom: 30upx;
	}
</style>
